/*
 * TowerGreenThree.java
 *
 * Created on December 16, 2007, 7:58 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package hextd.tower;

import hextd.Constants;
import hextd.hex.HexTile;
import hextd.creep.HexTDCreep;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author hylke
 */
public class TowerGreenThree extends TowerBase {

	public static final Map<String, Long> price = new HashMap<String, Long>();

	static {
		price.put(Constants.RESOURCE_CREDIT, 500000l);
	}
	public static int damage = 200000;
	public static double range = 4.5f;
	public static String name = "Green Three";
	//
	private HexTDCreep[] fireAt = {null, null, null};
	private float rJump = 2.5f;
	private Constants.COLOR damageColor = Constants.COLOR.GREEN;
	private Color lineColorDark;
	private Point2D.Double targetGLoc1 = new Point2D.Double();
	private Point2D.Double targetGLoc2 = new Point2D.Double();
	private Point2D.Double tempTargetPLoc1 = new Point2D.Double();
	private Point2D.Double tempTargetPLoc2 = new Point2D.Double();
	private Point2D.Double tempTargetPLoc3 = new Point2D.Double();

	/** Creates a new instance of TowerGreenThree */
	public TowerGreenThree() {
		super(FactoryTower.type.greenThree, price, damage, range);
		lineColor = Color.GREEN;
		hexInfluence = 3;
		lineColorDark = new Color(lineColor.getRed() / 2, lineColor.getGreen() / 2, lineColor.getBlue() / 2);
	}

	@Override
	public void runTick(int gameTime) {
		if (!lock) {
			fireAt[0] = null;
		}
		fireAt[1] = null;
		fireAt[2] = null;
		fireAt[0] = findEnemy(fireAt[0], false);
		if (fireAt[0] != null) {
			fireAt[0].getGameLocation(targetGLoc1);
			fireAt[0].doDamage(getOwner(), damageCurrent, damageColor);
			fireAt[1] = findEnemyClosest(targetGLoc1.x, targetGLoc1.y, rJump, fireAt);
			if (fireAt[1] != null) {
				fireAt[0].getGameLocation(targetGLoc2);
				fireAt[1].doDamage(getOwner(), damageCurrent, damageColor);
				fireAt[2] = findEnemyClosest(targetGLoc2.x, targetGLoc2.y, rJump, fireAt);
				if (fireAt[2] != null) {
					fireAt[2].doDamage(getOwner(), damageCurrent, damageColor);
				}
			}
		}
	}

	@Override
	public String getStatusString() {
		String retString = "";
		retString += "<h1>Green Tower 3</h1>"
				+ "<p>"
				+ "<b>Level:</b> " + (1 + level) + "<br>\n"
				+ "<b>Damage:</b> " + damageText + "<br>\n"
				+ "<b>Range:</b> " + rangeText + "<br>"
				+ "</p>\n";
		return retString;
	}

	@Override
	public String getDescription() {
		String retString = "";
		retString += "<h1>Green Tower 3</h1>"
				+ "<p>"
				+ "Tower with one strong laser that refracts from the first target to a second and a third. All three targets receive full damage.<br>"
				+ "Strong against green enemies, very weak against red enemies.</p>"
				+ "<p>"
				+ "<b>Price:</b> " + TowerGreenThree.form.format(TowerGreenThree.price) + "<br>"
				+ "<b>Damage:</b> " + TowerGreenThree.form.format(damageCurrent) + "<br>"
				+ "<b>Range:</b> " + TowerGreenThree.form.format(rangeCurrent) + "<br>"
				+ "<b>Refire rate:</b> 20/s"
				+ "</p>";
		return retString;
	}

	@Override
	protected void generateShapes() {
		double width = HexTile.relativeHexWidth;
		int shapeCount = 7;
		shapes = new Shape[shapeCount];
		double scale = paintScale;
		shapes[0] = new Ellipse2D.Double(-0.6 * scale, -0.6 * scale, 1.2 * scale, 1.2 * scale);
		shapes[1] = new Ellipse2D.Double(-0.4 * scale, -0.4 * scale, 0.8 * scale, 0.8 * scale);
		shapes[2] = new Ellipse2D.Double(-0.2 * scale, -0.2 * scale, 0.4 * scale, 0.4 * scale);
		shapes[3] = new Line2D.Double(0, 0.6 * scale, 0, scale);
		shapes[4] = new Line2D.Double(-0.6 * width, -0.5 * 0.6 * scale, -width, -0.5 * scale);
		shapes[5] = new Line2D.Double(0.6 * width, -0.5 * 0.6 * scale, width, -0.5 * scale);
		shapes[6] = new Ellipse2D.Double(-1.0 * strokeScale, -1.0 * strokeScale, 2.0 * strokeScale, 2.0 * strokeScale);

		shapeAction = new int[shapeCount];
		shapeAction[0] = 1;
		shapeAction[1] = 1;
		shapeAction[2] = 1;
		shapeAction[3] = 1;
		shapeAction[4] = 1;
		shapeAction[5] = 1;
		shapeAction[6] = 1;

		shapeColor = new Color[shapeCount];
		shapeColor[0] = lineColor;
		shapeColor[1] = lineColorDark;
		shapeColor[2] = lineColorDark;
		shapeColor[3] = lineColor;
		shapeColor[4] = lineColor;
		shapeColor[5] = lineColor;
		shapeColor[6] = lineColor;

		strokes = new Stroke[shapeCount];
		strokes[0] = new BasicStroke(2.0f * strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
		strokes[1] = strokes[0];
		strokes[2] = strokes[0];
		strokes[3] = strokes[0];
		strokes[4] = strokes[0];
		strokes[5] = strokes[0];
		strokes[6] = strokes[0];
	}

	@Override
	public void paintEffects(Graphics2D g2, int gameTime) {
		Stroke defaultStroke = g2.getStroke();
		HexTDCreep fa0 = fireAt[0];
		HexTDCreep fa1 = fireAt[1];
		HexTDCreep fa2 = fireAt[2];
		if (fa0 != null) {
			g2.setColor(lineColor);
			fa0.getPaintLocation(tempTargetPLoc1);
			double e1x = tempTargetPLoc1.x + ((Math.random() - 0.5) * paintScale / 10f);
			double e1y = tempTargetPLoc1.y + ((Math.random() - 0.5) * paintScale / 10f);
			g2.draw(new Line2D.Double(paintLocation.x, paintLocation.y, e1x, e1y));
			if (fa1 != null) {
				fa1.getPaintLocation(tempTargetPLoc2);
				double e2x = tempTargetPLoc2.x + ((Math.random() - 0.5) * paintScale / 10f);
				double e2y = tempTargetPLoc2.y + ((Math.random() - 0.5) * paintScale / 10f);
				g2.draw(new Line2D.Double(e2x, e2y, e1x, e1y));
				if (fa2 != null) {
					fa2.getPaintLocation(tempTargetPLoc3);
					double e3x = tempTargetPLoc3.x + ((Math.random() - 0.5) * paintScale / 10f);
					double e3y = tempTargetPLoc3.y + ((Math.random() - 0.5) * paintScale / 10f);
					g2.draw(new Line2D.Double(e2x, e2y, e3x, e3y));
				}
			}
		}

		g2.setStroke(defaultStroke);
	}
}
